package com.zhongwang.mms.module.basic.service;

import com.zhongwang.mms.gen.entity.TDictionary;
import com.zhongwang.mms.gen.entity.TDictionaryExample;
import com.zhongwang.mms.module.basic.dao.TDictionaryDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 数据字典区位 服务层
 * @Author: China.wsg
 * @Date: 2020/04/13 16:26
 */
@Service
public class TDictionaryService {

    @Autowired
    private TDictionaryDao dictionaryDao;

    /**
     * 取得数据字典
     *
     * @param dictionary 检索条件
     * @return
     */
    public List<TDictionary> findDictionary(TDictionary dictionary) {

        TDictionaryExample ex = new TDictionaryExample();
        ex.setOrderByClause("dic_order_no asc");
        TDictionaryExample.Criteria criteria = ex.createCriteria();
        criteria.andDicStatusNotEqualTo(0);  // 状态：0=删除 1=可用 2=失效

        if (dictionary.getDicName() != null && !"".equals(dictionary.getDicName())) {
            criteria.andDicNameLike("%" + dictionary.getDicName() + "%");
        }
        if (dictionary.getDicStatus() != null && !"".equals(dictionary.getDicStatus())) {
            criteria.andDicStatusEqualTo(dictionary.getDicStatus());
        }

        return dictionaryDao.selectByExample(ex);
    }

    /**
     * 保存字典信息
     *
     * @param dictionary 字典信息
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int save(TDictionary dictionary) {
        int row = dictionaryDao.insert(dictionary);
        return row;
    }

    /**
     * 按名称名查询字典
     *
     * @param name 字典名
     * @return 字典信息
     */
    public TDictionary findDictionaryByName(String name) {
        TDictionaryExample ex = new TDictionaryExample();
        TDictionaryExample.Criteria criteria = ex.createCriteria();
        criteria.andDicNameEqualTo(name);
        criteria.andDicStatusNotEqualTo(0);

        List<TDictionary> dictionaries = dictionaryDao.selectByExample(ex);
        if (dictionaries != null && dictionaries.size() > 0) {
            return dictionaries.get(0);
        }
        return null;
    }

    /**
     * 修改字典
     *
     * @param dictionary 字典信息
     * @return 修改行数
     */
    public int del(TDictionary dictionary) {

        int row = dictionaryDao.updateByPrimaryKeySelective(dictionary);

        // 删除下级
        TDictionary tDictionary = new TDictionary();
        tDictionary.setDicStatus(0);
        TDictionaryExample ex = new TDictionaryExample();
        TDictionaryExample.Criteria criteria = ex.createCriteria();
        criteria.andDicPidEqualTo(dictionary.getDicId());
        dictionaryDao.updateByExampleSelective(tDictionary, ex);

        return row;
    }

    /**
     * 根据主键查询字典
     *
     * @param id 主键
     * @return 字典
     */
    public TDictionary findDictionaryById(Integer id) {
        return dictionaryDao.selectByPrimaryKey(id);
    }

    /**
     * 修改字典
     *
     * @param dictionary 字典信息
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateDictionary(TDictionary dictionary) {
        int row = dictionaryDao.updateByPrimaryKeySelective(dictionary);
        return row;
    }


}
